<template>
  <!-- <div class="login-body"> -->
  <div class="login-body-div">
    <h1>用 户 登 录</h1>
    <div class="login-body-center">
      <div class="login-item-left">
        <!-- <div class="login-left-img-div"> -->
        <img src="http://192.168.2.27:8888/assets/img/login.png" alt="" />
        <!-- </div> -->
      </div>
      <div class="login-item">
        <input
          type="text"
          placeholder="请输入账号"
          v-model.trim="username"
          :class="[{ 'nologin animated shake': iserrorlogin }]"
        /><br />
        <input
          type="password"
          placeholder="请输入密码"
          v-model.trim="password"
          :class="[{ 'nologin animated shake': iserrorlogin }]"
        /><br />
        <button @click="open" class="login-btn">登&nbsp;&nbsp;&nbsp;录</button>
        <!-- 账号：admin &nbsp;&nbsp;&nbsp;&nbsp;密码：123456 -->
        <span class="tishi-p" @click="goregister()">立即注册</span>
      </div>
    </div>
    <slide-view v-if="isopen" @closeslide="login"></slide-view>
  </div>
  <!-- </div> -->
</template>

<script>
import SlideView from 'components/slidecomp/SlideView.vue'
import { ElMessage } from "element-plus";
import { ElNotification } from "element-plus";
import {
  checklogin,
  getuserinfo,
  changelogintime,
  changeoverduetime,
  changeuserlevel,
} from "network/request.js";

export default {
  name: "LoginView",
  components: {
    SlideView
  },
  data() {
    return {
      username: "",
      password: "",
      id: "",
      iserrorlogin: false,
      overtime: "",
      isopen: false
    };
  },
  watch: {
    username() {
      this.iserrorlogin = false;
    },
    password() {
      this.iserrorlogin = false;
    },
  },
  methods: {
    open() {
      this.isopen = true
    },
    login() {
      this.isopen = false
      if (this.username == "" || this.password == "") {
        this.errorinput("请输入账号密码！");
        return;
      }
      checklogin(this.username, this.password).then(({ data: res }) => {
        console.log(res);
        if (res.length == 0) {
          this.errorinput("请输入正确账号密码！");
          return;
        } else if (res[0].level == "禁用") {
          this.errorinput("当前账号已禁用！请联系客服处理");
          return;
        } else {
          // console.log(res[0].level);
          // ElMessage.success("true login!");
          // console.log(res[0].userid)
          let token = res[0].userid;
          localStorage.setItem("token", token);
          getuserinfo(token).then(({ data: res }) => {
            this.id = res[0].id;
            this.overtime = res[0].overduetime;
            let nowtime = new Date();
            let ovt = "";
            if (
              new Date(this.overtime).getTime() < nowtime.getTime() &&
              this.overtime != ""
            ) {
              changeuserlevel(this.id, "普通用户");
              changeoverduetime(this.id, ovt);
              ElNotification({
                title: "Info",
                message: "您的会员已过期！如需续费请前往会员中心.",
                type: "info",
              });
            }
            else {
              ElNotification({
                title: "success login",
                message: "欢迎登录！用户" + res[0].usernumber,
                type: "success",
              });
            }
            let logtime = new Date();
            let logintime = logtime.toLocaleString();
            changelogintime(this.id, logintime);
          });

          this.$router.push("/home");
        }
      });
    },
    goregister() {
      this.$emit("goregister");
    },
    errorinput(val) {
      if (this.iserrorlogin == false) {
        this.iserrorlogin = true;
        ElMessage.error(val);
        setTimeout(() => {
          this.iserrorlogin = false;
        }, 820);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.login-body-div {
  // max-width: 700px;
  width: 55%;
  height: 50%;
  margin: 0 auto;
}
@media screen and (max-width: 768px) {
  .login-body-div {
    height: 40%;
    width: 70%;
  }
}
@media screen and (min-width: 768px) and (max-width: 821px) {
  .login-body-div {
    width: 90%;
    height: 45%;
  }
}
h1 {
  margin-top: 8%;
  color: whitesmoke;
  text-align: center;
  margin-bottom: 30px;
  font-size: 50px;
  text-shadow: 4px 1px 8px rgb(250, 168, 182);
}
.login-body-center {
  display: flex;
  // max-width: 700px;
  width: 100%;
  height: 100%;
  margin: auto;
  box-shadow: 12px 10px 18px rgb(71, 71, 71);
}
.login-item-left {
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 8px 0 0px 8px;
  // width: 50%;
  flex: 1;
  position: relative;
}
.login-item {
  flex: 1;
  background-color: rgb(32, 39, 61);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  // margin-top: 10%;
  // width: 50%;
  margin: 0 auto;
  position: relative;
  box-sizing: border-box;
}
.login-item input {
  border: none;
  width: 80%;
  height: 45px;
  border-radius: 8px;
  font-size: 18px;
  color: #ccc;
  padding-left: 2%;
  outline: none;
  background-color: rgb(38, 46, 71);
}
.login-item .nologin {
  border: 2px solid #b22222;
  &::placeholder {
    color: #b22222;
  }
}
.login-item input:focus {
  border: 2px solid lightskyblue;
}
.login-btn {
  width: 50%;
  height: 40px;
  border: none;
  color: lightgreen;
  font-size: 14px;
  background-color: rgb(38, 46, 71);
  border: 2px solid rgba(93, 250, 93, 0.5);
  border-radius: 8px;
  box-sizing: border-box;
  position: relative;
  z-index: 100;
  transform-style: preserve-3d;
  &::after {
    content: "";
    left: 0;
    top: 0;
    position: absolute;
    width: 100%;
    height: 100%;

    border-radius: 6px;
    background-color: rgb(93, 250, 93);
    transform: scaleX(0);
    transform: rotateZ(-300px);
    transform-origin: left top;
    transition: 0.4s linear;
  }
  &:hover::after {
    color: white;
    transform: scaleX(1);
    transform: rotateZ(-300px);
    background-color: rgba(23, 255, 23, 0.5);
  }
  &:hover {
    color: rgb(0, 255, 0);
    font-weight: 800;
    transform: rotateZ(300px);
  }
}
input::placeholder {
  padding-left: 8px;
  color: rgb(126, 126, 126);
  font-size: 15px;
}
.tishi-p {
  color: #ccc;
  // margin-top: 20px;
  position: absolute;
  bottom: 1.4em;
  right: 2em;
  font-size: 13px;
  transform-origin: center;
  transition: 0.3s linear;
}
.tishi-p:hover {
  color: orange;
}
@media screen and (max-width: 768px) {
  .tishi-p {
    font-size: 12px;
    bottom: 1.2em;
  }
  .login-item-left {
    display: none;
  }
}
.login-left-img-div {
  background-color: #fff;
  width: 70%;
  height: 70%;
}
img {
  width: 30%;
  height: 30%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>